package tx.bj12320.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.activiti.engine.IdentityService;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bj12320.model.*;
import tx.bj12320.dao.*;
import tx.bj12320.service.*;
import tx.bj12320.util.Struts2Utils;
import tx.bj12320.util.WebUtils;
import tx.bj12320.vo.query.*;


public class OrgInfoAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/pages/OrgInfo/query.jsp";
	protected static final String LIST_JSP= "/pages/OrgInfo/list.jsp";
	protected static final String CREATE_JSP = "/pages/OrgInfo/create.jsp";
	protected static final String EDIT_JSP = "/pages/OrgInfo/edit.jsp";
	protected static final String SHOW_JSP = "/pages/OrgInfo/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/OrgInfo/list.do";

	private OrgInfoManager orgInfoManager;

	private OrgInfo orgInfo;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			orgInfo = new OrgInfo();
		} else {
			orgInfo = (OrgInfo)orgInfoManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setOrgInfoManager(OrgInfoManager manager) {
		this.orgInfoManager = manager;
	}

	public Object getModel() {
		return orgInfo;
	}

	public void setOrgInfoId(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		OrgInfoQuery query = newQuery(OrgInfoQuery.class,DEFAULT_SORT_COLUMNS);
		query.setOrgLevel(1L);
		Page page = orgInfoManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	public String createDepartment() {
		return "/pages/OrgInfo/create-department.jsp";
	}
	public String departmentList() {
		OrgInfoQuery query = newQuery(OrgInfoQuery.class,DEFAULT_SORT_COLUMNS);
		if (isNullOrEmptyString(id)) {
			query.setOrgInfoId(orgInfoManager.getLevel1OrgInfo(WebUtils.getUserInSession().getOrgInfo()).getOrgInfoId());
		}else{
			query.setOrgInfoId(id);
		}
		Page page = orgInfoManager.findPage(query);
		savePage(page,query);
		return "/pages/OrgInfo/department-list.jsp";
	}
	public String saveDepartment() {
		OrgInfo p_orgInfo=orgInfoManager.getById(orgInfo.getParentOrgInfoId());

		orgInfo.setSysUserType(p_orgInfo.getSysUserType());
		orgInfo.setOrgLevel(2L);
		orgInfoManager.save(orgInfo);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}
	private IdentityService identityService;

	public void setIdentityService(IdentityService identityService) {
		this.identityService = identityService;
	}

	/** 保存新增对象 */
	public String save() {
		orgInfoManager.save(orgInfo);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		orgInfoManager.update(this.orgInfo);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			Long id = new Long((String)params.get("orgInfoId"));
			orgInfoManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}
	public String outOrgInfo(){
		String term=this.getRequest().getParameter("term");
		List<OrgInfo> ois=orgInfoManager.findAllLikeByOrgName(term);
		List l=new ArrayList();
		for (OrgInfo oi : ois) {
			HashMap hm=new HashMap();
			hm.put("label", oi.getOrgName());
			hm.put("orgInfoId", oi.getOrgInfoId());
			l.add(hm);
		}
		Struts2Utils.renderJson(l);
		return null;
	}

	public String outOrgInfoTree(){
		String node=this.getRequest().getParameter("node");
		List<OrgInfo> ois=orgInfoManager.findDepartments(new Long(node));
		List l=new ArrayList();
		for (OrgInfo oi : ois) {
			HashMap hm=new HashMap();
			hm.put("text", oi.getOrgName());
			hm.put("id", oi.getOrgInfoId());
			hm.put("leaf", true);
			l.add(hm);
		}
		Struts2Utils.renderJson(l);
		return null;
	}

}
